/**
 * Copyright (c) 2018, wuxie All rights reserved.
 * qq:16349023,mail:16349023@qq.com
 * 未经许可禁止任何人通过任何渠道使用、修改源代码.
 * 项目名称 : wxcloud
 *
 * @version V1.0
 */
package cn.xo68.boot.auth.server.configuration;


import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;

/**
 *  shiro 会话
 * @author wuxie
 * @date 2018/8/6 21:29
 *
 */
//@Configuration
public class ShiroSessionConfiguration {

    /**
     * session 在缓存中的命名空间
     */
    public static final String SESSION_CACHE_NAMESPACE="session";

    /**
     * session dao
     * @return
     */
    @Bean
    public EnterpriseCacheSessionDAO enterpriseCacheSessionDAO(JavaUuidSessionIdGenerator sessionIdGenerator){
        EnterpriseCacheSessionDAO enterpriseCacheSessionDAO=new EnterpriseCacheSessionDAO();
        enterpriseCacheSessionDAO.setActiveSessionsCacheName(SESSION_CACHE_NAMESPACE);
        enterpriseCacheSessionDAO.setSessionIdGenerator(sessionIdGenerator);
        return enterpriseCacheSessionDAO;
    }

    /**
     * shiro session的管理
     */
    @Bean
    public SessionManager sessionManager(CacheManager cacheManager,
                                         EnterpriseCacheSessionDAO enterpriseCacheSessionDAO, SimpleCookie sessionIdCookie) {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 单位毫秒（60分钟）
        sessionManager.setGlobalSessionTimeout(60*60*1000);
        sessionManager.setSessionDAO(enterpriseCacheSessionDAO);
        sessionManager.setCacheManager(cacheManager);
        sessionManager.setSessionIdCookieEnabled(true);
        sessionManager.setSessionIdCookie(sessionIdCookie);
        sessionManager.setSessionIdUrlRewritingEnabled(false);

        sessionManager.setDeleteInvalidSessions(true);
        sessionManager.setSessionValidationSchedulerEnabled(false);
        return sessionManager;
    }
}
